package com.ivideo.avcore.rtmplive;

/**
 * @author：lxy
 * @decription:
 * @time：2025/7/2
 */
public class RtmpPublisher {

    private long cPtr;
    private long timeOffset;
    private volatile static RtmpPublisher instance;
    public static RtmpPublisher newInstance() {
        if (instance == null){
            synchronized (RtmpPublisher.class){
                if (instance == null){
                    instance = new RtmpPublisher();
                }
            }
        }
        return instance;
    }
    private RtmpPublisher(){}

    public int init(String url, int w, int h, int timeOut) {
        cPtr = RtmpUtils.init(url, w, h, timeOut);
        if (cPtr != 0) {
            return 0;
        }
        return -1;
    }

    public int sendSpsAndPps(byte[] sps, int spsLen, byte[] pps, int ppsLen, long timeOffset) {
        this.timeOffset = timeOffset;
        return RtmpUtils.sendSpsAndPps(cPtr, sps, spsLen, pps, ppsLen, 0);
    }

    public int sendVideoData(byte[] data, int len, long timestamp) {
        if(timestamp-timeOffset<=0){return -1;}
        return RtmpUtils.sendVideoData(cPtr, data, len, timestamp - timeOffset);
    }

    public int sendAacSpec(byte[] data, int len) {
        return RtmpUtils.sendAacSpec(cPtr, data, len);
    }

    public int sendAacData(byte[] data, int len, long timestamp) {
        if(timestamp-timeOffset<0){return -1;}
        return RtmpUtils.sendAacData(cPtr, data, len, timestamp - timeOffset);
    }

    public int stop() {
        try {
            return RtmpUtils.stop(cPtr);
        }finally {
            cPtr=0;
        }
    }

}
